package com.jxc.admin.controller;


import com.jxc.admin.bean.User;
import com.jxc.admin.exceptions.ParamsException;
import com.jxc.admin.model.RespBean;
import com.jxc.admin.service.IUserService;
import com.jxc.admin.service.impl.UserServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zcv
 */
@Controller
@RequestMapping("/user")
public class UserController {


    @Resource
    private IUserService userService;

   /* @RequestMapping("login")
    @ResponseBody
    public RespBean login(String userName, String password, HttpSession session){
        try {
            User user = userService.login(userName,password);
            session.setAttribute("user",user);
            return RespBean.success("用户登录成功!");
        } catch (ParamsException e) {
            e.printStackTrace();
            return RespBean.error(e.getMsg());
        }catch (Exception e) {
            e.printStackTrace();
            return RespBean.error("用户登录失败!");
        }
    }*/



    
   //用户信息设置页面
   @RequestMapping("setting")
   //HttpSession session 获取用户的会话信息
   public String setting(HttpSession session){
       //拿到当前会话的用户信息
       User user= (User) session.getAttribute("user");
       //对当前用户信息做一个查询，通过调用userService
       //把对应的用户id传进去  userService.getById(user.getId())
       //获取最新的用户信息
       session.setAttribute("user",userService.getById(user.getId()));

       return "user/setting";
   }

 //*用户信息更新*//*
   @RequestMapping("updateUserInfo")
   @ResponseBody //方法响应json值

   public RespBean updateUserInfo(User user){
       try {
           userService.updateUserInfo(user);
           return RespBean.success("用户信息更新成功");
       } catch (ParamsException e) {
           e.printStackTrace();
           return RespBean.error(e.getMsg());
       }catch (Exception e) {
           e.printStackTrace();
           return RespBean.error("用户信息更新失败!");
       }
   }




    /*用户密码更新页*/
   @RequestMapping("password")
    public String password(){
       return "user/password";
   }

  /*用户密码更新*/
   @RequestMapping("updateUserPassword")
   @ResponseBody
   //加入一个session，便于获取当前用户的信息
   public RespBean updateUserPassword(HttpSession session,String oldPassword,String newPassword,String confirmPassword){

       try {
           User user= (User) session.getAttribute("user");
           userService.updateUserPassword(user.getUsername(),oldPassword,newPassword,confirmPassword);
           return RespBean.success("用户密码更新成功");
       } catch (ParamsException e) {
           e.printStackTrace();
           return RespBean.error(e.getMsg());
       }catch (Exception e) {
           e.printStackTrace();
           return RespBean.error("用户密码更新失败!");
       }
   }

}
